package com.hongxun.budget.utils;

import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.Spinner;

import com.hongxun.budget.R;
import com.hongxun.budget.greendao.APCriteriaEntityDao;
import com.hongxun.budget.greendao.APRangeEntityDao;
import com.hongxun.budget.greendao.APWireEntityDao;
import com.hongxun.budget.greendao.BDataDictionaryEntityDao;
import com.hongxun.budget.greendao.BDeviceQuantitiesEntityDao;
import com.hongxun.budget.greendao.BEntryCriteriaEntityDao;
import com.hongxun.budget.greendao.BEstimateEntityDao;
import com.hongxun.budget.greendao.BMaterialEntityDao;
import com.hongxun.budget.greendao.BMaterialQuantitiesEntityDao;
import com.hongxun.budget.greendao.BOtherCostEntityDao;
import com.hongxun.budget.greendao.BOtherQuantitiesEntityDao;
import com.hongxun.budget.greendao.BPointConfigEntityDao;
import com.hongxun.budget.greendao.BProjectApplicationEntityDao;
import com.hongxun.budget.greendao.DDeviceBuyEntityDao;
import com.hongxun.budget.greendao.DDeviceOldEntityDao;
import com.hongxun.budget.greendao.DMaterialBuyEntityDao;
import com.hongxun.budget.greendao.DMaterialCostEntityDao;
import com.hongxun.budget.greendao.DMaterialOldEntityDao;
import com.hongxun.budget.greendao.DOtherCostEntityDao;
import com.hongxun.budget.greendao.DPictureEntityDao;
import com.hongxun.budget.greendao.DPointEntityDao;
import com.hongxun.budget.greendao.DWorkContentEntityDao;
import com.hongxun.budget.greendao.DaoSession;
import com.hongxun.budget.greendao.EDeviceBuyEntityDao;
import com.hongxun.budget.greendao.EDeviceOldEntityDao;
import com.hongxun.budget.greendao.EMaterialBuyEntityDao;
import com.hongxun.budget.greendao.EMaterialOldEntityDao;
import com.hongxun.budget.greendao.EPRangeEntityDao;
import com.hongxun.budget.greendao.EPWireEntityDao;
import com.hongxun.budget.greendao.EPointEntityDao;
import com.hongxun.budget.greendao.EProblemexistEntityDao;
import com.hongxun.budget.greendao.EWorkContentEntityDao;
import com.hongxun.budget.ui.activity.tk.AddDeviceActivity;
import com.hongxun.budget.ui.activity.tk.AddMaterialActivity;
import com.hongxun.budget.ui.activity.tk.AddQuantitiesActivity;
import com.hongxun.budget.ui.activity.tk.ChoicePointActivity;
import com.hongxun.budget.ui.activity.tk.EditDeviceActivity;
import com.hongxun.budget.ui.activity.tk.EditMaterialActivity;
import com.hongxun.budget.ui.activity.tk.EditOldMaterialActivity;
import com.hongxun.budget.ui.activity.tk.EditePointDeviceActivity;
import com.hongxun.budget.ui.activity.tk.MaterialModelActivity;
import com.hongxun.budget.ui.activity.tk.SearchDeviceActivity;
import com.hongxun.budget.ui.activity.tk.SearchMaterialActivity;
import com.hongxun.budget.ui.activity.tk.UserOldDeviceActivity;
import com.hongxun.budget.ui.activity.tk.UserOldMaterialActivity;
import com.hongxun.budget.ui.activity.ys.AddProblemActivity;
import com.hongxun.budget.ui.activity.ys.ChoiceYsPointActivity;
import com.hongxun.budget.ui.activity.ys.YsEditDeviceActivity;
import com.hongxun.budget.ui.activity.ys.YsEditMaterialActivity;
import com.hongxun.budget.ui.activity.ys.YsEditOldMaterialActivity;
import com.hongxun.budget.ui.activity.ys.YsEditePointDeviceActivity;
import com.hongxun.budget.ui.activity.ys.YsUserOldDeviceActivity;
import com.hongxun.budget.ui.activity.ys.YsUserOldMaterialActivity;
import com.hongxun.budget.ui.dialog.BottomClickDialog;
import com.hongxun.budget.ui.dialog.ListDialog;
import com.hongxun.budget.ui.fragment.ys.YsLineDeviceFragment;
import com.hongxun.budget.ui.fragment.ys.YsMaterialFragment;
import com.hongxun.entity.LineName;
import com.hongxun.greenentity.tk.APBaseEntity;
import com.hongxun.greenentity.tk.APCriteriaEntity;
import com.hongxun.greenentity.tk.APRangeEntity;
import com.hongxun.greenentity.tk.APWireEntity;
import com.hongxun.greenentity.tk.BDataDictionaryEntity;
import com.hongxun.greenentity.tk.BDeviceEntity;
import com.hongxun.greenentity.tk.BDeviceQuantitiesEntity;
import com.hongxun.greenentity.tk.BEntryCriteriaEntity;
import com.hongxun.greenentity.tk.BEstimateEntity;
import com.hongxun.greenentity.tk.BMaterialEntity;
import com.hongxun.greenentity.tk.BMaterialQuantitiesEntity;
import com.hongxun.greenentity.tk.BOtherCostEntity;
import com.hongxun.greenentity.tk.BOtherQuantitiesEntity;
import com.hongxun.greenentity.tk.BPointConfigEntity;
import com.hongxun.greenentity.tk.BProjectApplicationEntity;
import com.hongxun.greenentity.tk.DCrossCostEntity;
import com.hongxun.greenentity.tk.DDeviceBuyEntity;
import com.hongxun.greenentity.tk.DDeviceOldEntity;
import com.hongxun.greenentity.tk.DMaterialBuyEntity;
import com.hongxun.greenentity.tk.DMaterialCostEntity;
import com.hongxun.greenentity.tk.DMaterialOldEntity;
import com.hongxun.greenentity.tk.DOtherCostEntity;
import com.hongxun.greenentity.tk.DPictureEntity;
import com.hongxun.greenentity.tk.DPointEntity;
import com.hongxun.greenentity.tk.DWorkContentEntity;
import com.hongxun.greenentity.ys.ECrossCostEntity;
import com.hongxun.greenentity.ys.EDeviceBuyEntity;
import com.hongxun.greenentity.ys.EDeviceOldEntity;
import com.hongxun.greenentity.ys.EMaterialBuyEntity;
import com.hongxun.greenentity.ys.EMaterialOldEntity;
import com.hongxun.greenentity.ys.EPBaseEntity;
import com.hongxun.greenentity.ys.EPRangeEntity;
import com.hongxun.greenentity.ys.EPWireEntity;
import com.hongxun.greenentity.ys.EPointEntity;
import com.hongxun.greenentity.ys.EProblemexistEntity;
import com.hongxun.greenentity.ys.EWorkContentEntity;
import com.hongxun.interfaces.ResponseListener;

import org.greenrobot.greendao.query.QueryBuilder;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

/**
 * Created by admin on 2018/8/3.
 */

public class DBUtils {

    /**
     * 获取项目基本信息
     *
     * @param context
     * @return
     */
    public static APBaseEntity getProject(Context context) {
        String guid = (String) SPUtils.get(context, "project_guid", "");
        return GreenDaoManager.getInstance().getmDaoSession().getAPBaseEntityDao().load(guid);
    }

    public static EPBaseEntity getYsProject(Context context) {
        String guid = (String) SPUtils.get(context, "project_ys_guid", "");
        return GreenDaoManager.getInstance().getmDaoSession().getEPBaseEntityDao().load(guid);
    }

    /**
     * 根据项目类型查询-项目申请书
     *
     * @param dKey-项目类型数据字典
     * @return
     */
    public static BProjectApplicationEntity getBProjectApplicationEntity(String dKey) {
        return GreenDaoManager.getInstance().getmDaoSession().getBProjectApplicationEntityDao().queryBuilder().
                where(BProjectApplicationEntityDao.Properties.PAType.like("%" + dKey + "%")).unique();
    }

    /**
     * 根据项目类型查询-项目简表
     *
     * @param dKey
     * @return
     */
    public static List<BEstimateEntity> getBEstimateEntities(String dKey) {
        return GreenDaoManager.getInstance().getmDaoSession().getBEstimateEntityDao().
                queryBuilder().where(BEstimateEntityDao.Properties.AGuid.eq(getBProjectApplicationEntity(dKey).getGuid())
                , BEstimateEntityDao.Properties.EType.eq(dKey)).list();
    }

    /**
     * 根据code查询-数据字典
     *
     * @param code
     * @return
     */
    public static List<BDataDictionaryEntity> getBDataDictionaryEntities(String code) {
        return GreenDaoManager.getInstance().getmDaoSession().getBDataDictionaryEntityDao().queryBuilder().where
                (BDataDictionaryEntityDao.Properties.DCode.eq(code)).list();
    }

    /**
     * 根据code和dKey查询-数据字典
     *
     * @param code
     * @return
     */
    public static BDataDictionaryEntity getBDataDictionaryEntity(String code, String dKey) {
        return GreenDaoManager.getInstance().getmDaoSession().getBDataDictionaryEntityDao().queryBuilder().where
                (BDataDictionaryEntityDao.Properties.DCode.eq(code), BDataDictionaryEntityDao.Properties.DKey.eq(dKey)).unique();
    }

    /**
     * 获取其他费用模板
     *
     * @param aGuid
     * @return
     */
    public static List<BOtherCostEntity> getOtherCostEntities(String aGuid) {
        return GreenDaoManager.getInstance().getmDaoSession().getBOtherCostEntityDao().queryBuilder()
                .where(BOtherCostEntityDao.Properties.AGuid.eq(aGuid)).build().list();
    }

    /**
     * 根据项目guid获取准入条件
     *
     * @param guid
     * @return
     */
    public static List<BEntryCriteriaEntity> getEntryCriteriaGuids(String guid) {
        DaoSession session = GreenDaoManager.getInstance().getmDaoSession();
        List<String> list = new ArrayList<>();
        List<APCriteriaEntity> entities = session.getAPCriteriaEntityDao().queryBuilder()
                .where(APCriteriaEntityDao.Properties.PGuid.eq(guid), APCriteriaEntityDao.Properties.PCDatastate.notEq("删除")).list();
        for (APCriteriaEntity b : entities) {
            list.add(b.getPCGuid());
        }
        return session.getBEntryCriteriaEntityDao().queryBuilder().where(BEntryCriteriaEntityDao.Properties.Guid.in(list)).list();
    }

    /**
     * 根据项目guid,准入条件guid-删除获取准入条件
     *
     * @param guid-项目guid
     * @param eGuid-准入条件guid
     * @return
     */
    public static void deleteEntryCriteria(String guid, String eGuid) {
        DaoSession session = GreenDaoManager.getInstance().getmDaoSession();
        APCriteriaEntity unique = session.getAPCriteriaEntityDao().queryBuilder()
                .where(APCriteriaEntityDao.Properties.PGuid.eq(guid), APCriteriaEntityDao.Properties.PCDatastate.notEq("删除")
                        , APCriteriaEntityDao.Properties.PCGuid.eq(eGuid)).unique();
        if (unique != null) {
            unique.setPCDatastate("删除");
            session.getAPCriteriaEntityDao().update(unique);
        }
    }

    /**
     * 根据项目guid获取线路台区
     *
     * @param guid
     * @return
     */
    public static List<LineName> getLineNames(String guid) {
        List<APRangeEntity> wireEntities = GreenDaoManager.getInstance().getmDaoSession().getAPRangeEntityDao().queryBuilder()
                .where(APRangeEntityDao.Properties.PGuid.eq(guid)).list();
        List<LineName> lineNames = new ArrayList<>();
        for (APRangeEntity a : wireEntities) {
            LineName l = new LineName(a.getGuid(), a.getPRName(), a.getPRType(), a.getPRVoltage());
            lineNames.add(l);
        }
        return lineNames;
    }

    /**
     * 根据项目guid获取线路台区
     *
     * @param guid
     * @return
     */
    public static List<LineName> getYsLineNames(String guid) {
        List<EPRangeEntity> wireEntities = GreenDaoManager.getInstance().getmDaoSession().getEPRangeEntityDao().queryBuilder()
                .where(EPRangeEntityDao.Properties.PGuid.eq(guid)).list();
        List<LineName> lineNames = new ArrayList<>();
        for (EPRangeEntity a : wireEntities) {
            LineName l = new LineName(a.getGuid(), a.getPRName(), a.getPRType(), a.getPRVoltage());
            lineNames.add(l);
        }
        return lineNames;
    }

    /**
     * 根据项目guid获取导线
     *
     * @param guid
     * @return
     */
    public static List<BMaterialEntity> getMaterialEntities(String guid) {
        DaoSession session = GreenDaoManager.getInstance().getmDaoSession();
        List<APWireEntity> entities = session.getAPWireEntityDao().queryBuilder()
                .where(APWireEntityDao.Properties.PGuid.eq(guid)).list();
        List<String> list = new ArrayList<>();
        for (APWireEntity a : entities) {
            list.add(a.getPMGuid());
        }
        return session.getBMaterialEntityDao().queryBuilder().where(BMaterialEntityDao.Properties.Guid.in(list)).list();
    }

    public static List<BMaterialEntity> getYsMaterialEntities(String guid) {
        DaoSession session = GreenDaoManager.getInstance().getmDaoSession();
        List<EPWireEntity> entities = session.getEPWireEntityDao().queryBuilder()
                .where(EPWireEntityDao.Properties.PGuid.eq(guid)).list();
        List<String> list = new ArrayList<>();
        for (EPWireEntity a : entities) {
            list.add(a.getPMGuid());
        }
        return session.getBMaterialEntityDao().queryBuilder().where(BMaterialEntityDao.Properties.Guid.in(list)).list();
    }

    /**
     * 获取项目是否作图
     *
     * @param context
     * @return
     */
    public static boolean getIsDraw(Context context) {
        return getProject(context).getIsDraw().equals("是");
    }

    public static boolean getYsIsDraw(Context context) {
        return getYsProject(context).getIsDraw().equals("是");
    }

    /**
     * 设置点位编码自增
     */
    public static void setPointCode(Context context, EditText pointCode) {
        DPointEntity unique = getLastPoint(context);
        if (unique == null)
            pointCode.setText("#1");
        else {
            String oldCode = unique.getPPCode();
            String[] split = oldCode.split("#");
            pointCode.setText(split[0] + "#" + increase(split[1]));
        }
    }

    /**
     * 设置默认线路
     */
    public static void setDefaultLineName(Context context, Spinner spinner) {
        DPointEntity lastPoint = DBUtils.getLastPoint(context);
        List<LineName> lineNames = DBUtils.getLineNames(SPUtils.get(context, "project_guid", "").toString());
        if (lastPoint != null) {//设备默认线路
            for (int i = 0; i < lineNames.size(); i++) {
                if (lastPoint.getPRGuid().equals(lineNames.get(i).getGuid())) {
                    spinner.setSelection(i);
                    break;
                }
            }
        }
    }

    /**
     * 设置默认线路
     */
    public static void setYsDefaultLineName(Context context, Spinner spinner) {
        EPointEntity lastPoint = DBUtils.getLastYsPoint(context);
        List<LineName> lineNames = DBUtils.getYsLineNames(SPUtils.get(context, "project_ys_guid", "").toString());
        if (lastPoint != null) {//设备默认线路
            for (int i = 0; i < lineNames.size(); i++) {
                if (lastPoint.getPRGuid().equals(lineNames.get(i).getGuid())) {
                    spinner.setSelection(i);
                    break;
                }
            }
        }
    }

    /**
     * 设置默认线路
     */
    public static void setDefaultVoltage(Context context, Spinner spinner) {
        DPointEntity lastPoint = DBUtils.getLastPoint(context);
        List<BDataDictionaryEntity> types = DBUtils.getBDataDictionaryEntities("voltage_type");
        if (lastPoint != null) {//设备默认线路
            for (int i = 0; i < types.size(); i++) {
                if (lastPoint.getPPVoltage().equals(types.get(i).getDKey())) {
                    spinner.setSelection(i);
                    break;
                }
            }
        }
    }

    /**
     * 设置默认线路
     */
    public static void setYsDefaultVoltage(Context context, Spinner spinner) {
        EPointEntity lastPoint = DBUtils.getLastYsPoint(context);
        List<BDataDictionaryEntity> types = DBUtils.getBDataDictionaryEntities("voltage_type");
        if (lastPoint != null) {//设备默认线路
            for (int i = 0; i < types.size(); i++) {
                if (lastPoint.getPPVoltage().equals(types.get(i).getDKey())) {
                    spinner.setSelection(i);
                    break;
                }
            }
        }
    }

    /**
     * 设置默认杆塔类型
     */
    public static void setDefaultGtType(Context context, Spinner spinner) {
        DPointEntity lastPoint = DBUtils.getLastGtPoint(context);
        List<BDataDictionaryEntity> types = DBUtils.getBDataDictionaryEntities("gt_type");
        if (lastPoint != null) {//设备默认线路
            for (int i = 0; i < types.size(); i++) {
                if (lastPoint.getPPPropertyone().equals(types.get(i).getDKey())) {
                    spinner.setSelection(i);
                    break;
                }
            }
        }
    }

    /**
     * 设置默认杆塔类型
     */
    public static void setYsDefaultGtType(Context context, Spinner spinner) {
        EPointEntity lastPoint = DBUtils.getYsLastGtPoint(context);
        List<BDataDictionaryEntity> types = DBUtils.getBDataDictionaryEntities("gt_type");
        if (lastPoint != null) {//设备默认线路
            for (int i = 0; i < types.size(); i++) {
                if (lastPoint.getPPPropertyone().equals(types.get(i).getDKey())) {
                    spinner.setSelection(i);
                    break;
                }
            }
        }
    }

    /**
     * 设置默认杆塔高度
     */
    public static void setDefaultGtHeight(Context context, Spinner spinner) {
        DPointEntity lastPoint = DBUtils.getLastGtPoint(context);
        List<BDataDictionaryEntity> types = DBUtils.getBDataDictionaryEntities("gt_height");
        if (lastPoint != null) {//设备默认线路
            for (int i = 0; i < types.size(); i++) {
                if (lastPoint.getPPBar() == Double.parseDouble(types.get(i).getDValue())) {
                    spinner.setSelection(i);
                    break;
                }
            }
        }
    }

    /**
     * 设置默认杆塔高度
     */
    public static void setYsDefaultGtHeight(Context context, Spinner spinner) {
        EPointEntity lastPoint = DBUtils.getYsLastGtPoint(context);
        List<BDataDictionaryEntity> types = DBUtils.getBDataDictionaryEntities("gt_height");
        if (lastPoint != null) {//设备默认线路
            for (int i = 0; i < types.size(); i++) {
                if (lastPoint.getPPBar() == Double.parseDouble(types.get(i).getDValue())) {
                    spinner.setSelection(i);
                    break;
                }
            }
        }
    }

    /**
     * 设置默认杆塔材质
     */
    public static void setDefaultGtMaterial(Context context, Spinner spinner) {
        DPointEntity lastPoint = DBUtils.getLastGtPoint(context);
        List<BDataDictionaryEntity> types = DBUtils.getBDataDictionaryEntities("gt_material");
        if (lastPoint != null) {//设备默认线路
            for (int i = 0; i < types.size(); i++) {
                if (lastPoint.getPPMaterial().equals(types.get(i).getDKey())) {
                    spinner.setSelection(i);
                    break;
                }
            }
        }
    }

    /**
     * 设置默认杆塔材质
     */
    public static void setYsDefaultGtMaterial(Context context, Spinner spinner) {
        EPointEntity lastPoint = DBUtils.getYsLastGtPoint(context);
        List<BDataDictionaryEntity> types = DBUtils.getBDataDictionaryEntities("gt_material");
        if (lastPoint != null) {//设备默认线路
            for (int i = 0; i < types.size(); i++) {
                if (lastPoint.getPPMaterial().equals(types.get(i).getDKey())) {
                    spinner.setSelection(i);
                    break;
                }
            }
        }
    }

    /**
     * 设置默认杆塔材质
     */
    public static void setDefaultDxType(Context context, Spinner spinner) {
        DPointEntity lastPoint = DBUtils.getLastGtOrZsbyqPoint(context);
        List<BMaterialEntity> materialEntities = DBUtils.getMaterialEntities((String) SPUtils.get(context, "project_guid", ""));
        if (lastPoint != null) {//设备默认线路
            for (int i = 0; i < materialEntities.size(); i++) {
                if (lastPoint.getPWGuid().equals(materialEntities.get(i).getGuid())) {
                    spinner.setSelection(i);
                    break;
                }
            }
        }
    }

    /**
     * 设置默认杆塔材质
     */
    public static void setYsDefaultDxType(Context context, Spinner spinner) {
        EPointEntity lastPoint = DBUtils.getYsLastGtOrZsbyqPoint(context);
        List<BMaterialEntity> materialEntities = DBUtils.getYsMaterialEntities((String) SPUtils.get(context, "project_ys_guid", ""));
        if (lastPoint != null) {//设备默认线路
            for (int i = 0; i < materialEntities.size(); i++) {
                if (lastPoint.getPWGuid().equals(materialEntities.get(i).getGuid())) {
                    spinner.setSelection(i);
                    break;
                }
            }
        }
    }

    /**
     * 设置默认杆塔材质
     */
    public static void setDefaultDxHeight(Context context, Spinner spinner) {
        DPointEntity lastPoint = DBUtils.getLastGtOrZsbyqPoint(context);
        List<String> strings = Arrays.asList(context.getResources().getStringArray(R.array.daoxian));
        if (lastPoint != null) {//设备默认线路
            for (int i = 0; i < strings.size(); i++) {
                if (lastPoint.getPPLinenum() == Integer.parseInt(strings.get(i))) {
                    spinner.setSelection(i);
                    break;
                }
            }
        }
    }

    /**
     * 设置默认杆塔材质
     */
    public static void setYsDefaultDxHeight(Context context, Spinner spinner) {
        EPointEntity lastPoint = DBUtils.getYsLastGtOrZsbyqPoint(context);
        List<String> strings = Arrays.asList(context.getResources().getStringArray(R.array.daoxian));
        if (lastPoint != null) {//设备默认线路
            for (int i = 0; i < strings.size(); i++) {
                if (lastPoint.getPPLinenum() == Integer.parseInt(strings.get(i))) {
                    spinner.setSelection(i);
                    break;
                }
            }
        }
    }

    /**
     * 获取最后一个杆塔
     *
     * @param context
     * @return
     */
    public static DPointEntity getLastGtPoint(Context context) {
        List<DPointEntity> list = GreenDaoManager.getInstance().getmDaoSession().getDPointEntityDao().queryBuilder()
                .where(DPointEntityDao.Properties.PGuid.eq((String) SPUtils.get(context, "project_guid", ""))
                        , DPointEntityDao.Properties.PPName.eq("杆塔"))
                .orderDesc(DPointEntityDao.Properties.PPCollecttime).list();
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    /**
     * 获取最后一个杆塔
     *
     * @param context
     * @return
     */
    public static EPointEntity getYsLastGtPoint(Context context) {
        List<EPointEntity> list = GreenDaoManager.getInstance().getmDaoSession().getEPointEntityDao().queryBuilder()
                .where(EPointEntityDao.Properties.PGuid.eq((String) SPUtils.get(context, "project_ys_guid", ""))
                        , EPointEntityDao.Properties.PPName.eq("杆塔"))
                .orderDesc(EPointEntityDao.Properties.PPCollecttime).list();
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    /**
     * 获取最后一个杆塔
     *
     * @param context
     * @return
     */
    public static DPointEntity getLastGtOrZsbyqPoint(Context context) {
        QueryBuilder<DPointEntity> builder = GreenDaoManager.getInstance().getmDaoSession().getDPointEntityDao().queryBuilder();
        List<DPointEntity> list = builder.where(DPointEntityDao.Properties.PGuid.eq((String) SPUtils.get(context, "project_guid", ""))
                , DPointEntityDao.Properties.PPWorktype.eq("新增")
                , builder.or(DPointEntityDao.Properties.PPName.eq("杆塔"), DPointEntityDao.Properties.PPName.eq("柱上变压器")))
                .orderDesc(DPointEntityDao.Properties.PPCollecttime).list();
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    /**
     * 获取最后一个杆塔
     *
     * @param context
     * @return
     */
    public static EPointEntity getYsLastGtOrZsbyqPoint(Context context) {
        QueryBuilder<EPointEntity> builder = GreenDaoManager.getInstance().getmDaoSession().getEPointEntityDao().queryBuilder();
        List<EPointEntity> list = builder.where(EPointEntityDao.Properties.PGuid.eq((String) SPUtils.get(context, "project_ys_guid", ""))
                , EPointEntityDao.Properties.PPWorktype.eq("新增")
                , builder.or(EPointEntityDao.Properties.PPName.eq("杆塔"), EPointEntityDao.Properties.PPName.eq("柱上变压器")))
                .orderDesc(EPointEntityDao.Properties.PPCollecttime).list();
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    /**
     * 设置点位编码自增
     */
    public static void setYsPointCode(Context context, EditText pointCode) {
        EPointEntity unique = getLastYsPoint(context);
        if (unique == null)
            pointCode.setText("#1");
        else {
            String oldCode = unique.getPPCode();
            String[] split = oldCode.split("#");
            pointCode.setText(split[0] + "#" + increase(split[1]));
        }
    }

    /**
     * 查询最后一点
     *
     * @param context
     * @return
     */
    public static DPointEntity getLastPoint(Context context) {
        List<DPointEntity> list = GreenDaoManager.getInstance().getmDaoSession().getDPointEntityDao().queryBuilder()
                .where(DPointEntityDao.Properties.PGuid.eq((String) SPUtils.get(context, "project_guid", "")))
                .orderDesc(DPointEntityDao.Properties.PPCollecttime).list();
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    /**
     * 查询最后一点
     *
     * @param context
     * @return
     */
    public static EPointEntity getLastYsPoint(Context context) {
        List<EPointEntity> list = GreenDaoManager.getInstance().getmDaoSession().getEPointEntityDao().queryBuilder()
                .where(EPointEntityDao.Properties.PGuid.eq((String) SPUtils.get(context, "project_ys_guid", "")))
                .orderDesc(EPointEntityDao.Properties.PPCollecttime).list();
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    /**
     * 数字自增
     *
     * @param value
     * @return
     */
    private static String increase(String value) {
        int index = 0;
        int n = Integer.parseInt(value.substring(index)) + 1;
        String newValue = String.valueOf(n);
        int len = value.length() - newValue.length() - index;

        for (int i = 0; i < len; i++) {
            newValue = "0" + newValue;
        }
        return value.substring(0, index) + newValue;
    }


    /**
     * 删除新设备工程量
     *
     * @param context
     * @param guid    选择材料或设备在购买表或旧设备表的GUID
     */
    public static void deleteCost(Context context, String guid) {
        //先删除存在的工程量信息,再插入新的工程量信息
        DMaterialCostEntityDao dao = GreenDaoManager.getInstance().getmDaoSession().getDMaterialCostEntityDao();
        List<DMaterialCostEntity> list = dao.queryBuilder()
                .where(DMaterialCostEntityDao.Properties.PMGuid.eq(guid)
                        , DMaterialCostEntityDao.Properties.PGuid.eq(SPUtils.get(context, "project_guid", ""))
                        , DMaterialCostEntityDao.Properties.PPGuid.eq(SPUtils.get(context, "UUID", ""))
                        , DMaterialCostEntityDao.Properties.MCDatastate.notEq("删除"))
                .list();
        for (DMaterialCostEntity m : list) {
            m.setMCDatastate("删除");
        }
        dao.updateInTx(list);//删除
    }


    /**
     * 添加新设备工程量
     *
     * @param context
     * @param deviceGuid 关联设备或材料GUID
     * @param guid       关联该选择设备或材料GUID
     * @param work       1-安装 2-拆除 3-大修
     * @param name       设备或材料名称
     * @param type       0-设备 1-材料
     * @param source     1 2 设备旧新 3 4 材料旧新
     * @param count      采集数量
     */
    public static void addDeviceCost(Context context, String deviceGuid, String guid, String name, int type, int source, int work, Double count) {
        List<BDeviceQuantitiesEntity> list = GreenDaoManager.getInstance().getmDaoSession().getBDeviceQuantitiesEntityDao().queryBuilder()
                .where(BDeviceQuantitiesEntityDao.Properties.DQWork.eq("" + work)
                        , BDeviceQuantitiesEntityDao.Properties.DQType.eq(getProject(context).getPBType())
                        , BDeviceQuantitiesEntityDao.Properties.DGuid.eq(deviceGuid)).list();
        for (BDeviceQuantitiesEntity b : list) {
            DMaterialCostEntity m = new DMaterialCostEntity();
            m.setGuid(UUID.randomUUID().toString());
            m.setPGuid(SPUtils.get(context, "project_guid", "").toString());
            m.setPPGuid(SPUtils.get(context, "UUID", "").toString());
            m.setPMGuid(guid);
            m.setMMName(name);
            m.setMMType(type);
            m.setPMSource(source);
            m.setMMGuid(deviceGuid);
            m.setMCWork(b.getDQWork());
            m.setMCQuantities(b.getDQQuantities());
            m.setMCExplain(b.getDQExplain());
            m.setMCUnit(b.getDQUnit());
            m.setMCMeterUnit(b.getDQMeterUnit());
            m.setMCCount(count);
            m.setMCPrice(b.getDQPrice());
            m.setMCPriceall(b.getDQPrice());
            m.setMCRatio(b.getDQRatio());
            m.setMCDatastate("新增");
            GreenDaoManager.getInstance().getmDaoSession().getDMaterialCostEntityDao().insertOrReplace(m);
        }
    }

    /**
     * 添加材料工程量
     *
     * @param context
     * @param deviceGuid 关联设备或材料GUID
     * @param guid       关联该选择设备或材料GUID
     * @param work       1-安装 2-拆除 3-更换
     * @param count      采集数量
     */
    public static void addMaterialCost(Context context, String deviceGuid, String guid, String name, int type, int source, int work, Double count) {
        List<BMaterialQuantitiesEntity> list = GreenDaoManager.getInstance().getmDaoSession().getBMaterialQuantitiesEntityDao().queryBuilder()
                .where(BMaterialQuantitiesEntityDao.Properties.MQWork.eq("" + work)
                        , BMaterialQuantitiesEntityDao.Properties.MQType.eq(getProject(context).getPBType())
                        , BMaterialQuantitiesEntityDao.Properties.MGuid.eq(deviceGuid)).list();
        for (BMaterialQuantitiesEntity b : list) {
            DMaterialCostEntity m = new DMaterialCostEntity();
            m.setGuid(UUID.randomUUID().toString());
            m.setPGuid(SPUtils.get(context, "project_guid", "").toString());
            m.setPPGuid(SPUtils.get(context, "UUID", "").toString());
            m.setPMGuid(guid);
            m.setMMName(name);
            m.setMMType(type);
            m.setPMSource(source);
            m.setMMGuid(deviceGuid);
            m.setMCWork(b.getMQWork());
            m.setMCQuantities(b.getMQQuantities());
            m.setMCExplain(b.getMQExplain());
            m.setMCUnit(b.getMQUnit());
            m.setMCMeterUnit(b.getMQMeterUnit());
            m.setMCCount(count);
            m.setMCPrice(b.getMQPrice());
            m.setMCPriceall((double) Math.round(b.getMQPrice() * count * 100) / 100);
            m.setMCRatio(b.getMQRatio());
            m.setMCDatastate("新增");
            GreenDaoManager.getInstance().getmDaoSession().getDMaterialCostEntityDao().insertOrReplace(m);
        }
    }

    public static DDeviceOldEntity setOldDevice(Context context, DDeviceOldEntity d, BDeviceEntity b, String process, boolean isUpdate) {
        if (d == null) {
            d = new DDeviceOldEntity();
            d.setGuid(UUID.randomUUID().toString());
            d.setPGuid((String) SPUtils.get(context, "project_guid", ""));
            d.setPPGuid((String) SPUtils.get(context, "UUID", ""));
            d.setDRWork(process);
        }
        d.setDLabel(b.getDLabel());
        d.setDDGuid(b.getGuid());
        d.setDRBigtype(b.getDBigtype());
        d.setDRMidtype(b.getDMidtype());
        d.setDRName(b.getDName());
        d.setDRParameter(b.getDParameter());
        d.setDRVoltage(b.getDVoltage());
        //设备厂家 出厂日期 投运日期 固定资产卡片编号 拟拆除日期 处置去向(no) 资产原值
        String sp = (String) SPUtils.get(context, "opinion_type", "");
        d.setDROpinion(sp.equals("") ? DBUtils.getBDataDictionaryEntities("opinion_type").get(0).getDKey() : sp);
        d.setDRDatastate(isUpdate ? "修改" : "新增");
        return d;
    }

    public static EDeviceOldEntity setYsOldDevice(Context context, EDeviceOldEntity d, BDeviceEntity b, String process, boolean isUpdate) {
        if (d == null) {
            d = new EDeviceOldEntity();
            d.setGuid(UUID.randomUUID().toString());
            d.setPGuid((String) SPUtils.get(context, "project_ys_guid", ""));
            d.setPPGuid((String) SPUtils.get(context, "UUID", ""));
            d.setDRWork(process);
        }
        d.setDLabel(b.getDLabel());
        d.setDDGuid(b.getGuid());
        d.setDRBigtype(b.getDBigtype());
        d.setDRMidtype(b.getDMidtype());
        d.setDRName(b.getDName());
        d.setDRParameter(b.getDParameter());
        d.setDRVoltage(b.getDVoltage());
        //设备厂家 出厂日期 投运日期 固定资产卡片编号 拟拆除日期 处置去向(no) 资产原值
        String sp = (String) SPUtils.get(context, "opinion_type", "");
        d.setDROpinion(sp.equals("") ? DBUtils.getBDataDictionaryEntities("opinion_type").get(0).getDKey() : sp);
        d.setDRDatastate(isUpdate ? "修改" : "新增");
        return d;
    }

    public static DDeviceBuyEntity setAddDevice(Context context, DDeviceBuyEntity d, BDeviceEntity b, boolean isUpdate) {
        if (d == null) {
            d = new DDeviceBuyEntity();
            d.setGuid(UUID.randomUUID().toString());
            d.setPGuid((String) SPUtils.get(context, "project_guid", ""));
            d.setPPGuid((String) SPUtils.get(context, "UUID", ""));
        }
        d.setDLabel(b.getDLabel());
        d.setDDGuid(b.getGuid());
        d.setDBBigtype(b.getDBigtype());
        d.setDBMidtype(b.getDMidtype());
        d.setDBName(b.getDName());
        d.setDBParameter(b.getDParameter());
        d.setDBVoltage(b.getDVoltage());
        d.setDBWeight(b.getDWeight());
        d.setDBPrice(b.getDPrice());
        d.setDBUnit(b.getDUnit());
        d.setDBCount(1);
        d.setDBWeightall(b.getDWeight());
        d.setDBPriceall(b.getDPrice() == null ? 0 : b.getDPrice());
        d.setDBDatastate(isUpdate ? "修改" : "新增");
        return d;
    }

    public static EDeviceBuyEntity setYsAddDevice(Context context, EDeviceBuyEntity d, BDeviceEntity b, boolean isUpdate) {
        if (d == null) {
            d = new EDeviceBuyEntity();
            d.setGuid(UUID.randomUUID().toString());
            d.setPGuid((String) SPUtils.get(context, "project_ys_guid", ""));
            d.setPPGuid((String) SPUtils.get(context, "UUID", ""));
        }
        d.setDLabel(b.getDLabel());
        d.setDDGuid(b.getGuid());
        d.setDBBigtype(b.getDBigtype());
        d.setDBMidtype(b.getDMidtype());
        d.setDBName(b.getDName());
        d.setDBParameter(b.getDParameter());
        d.setDBVoltage(b.getDVoltage());
        d.setDBWeight(b.getDWeight());
        d.setDBPrice(b.getDPrice());
        d.setDBUnit(b.getDUnit());
        d.setDBCount(1);
        d.setDBWeightall(b.getDWeight());
        d.setDBPriceall(b.getDPrice() == null ? 0 : b.getDPrice());
        d.setDBDatastate(isUpdate ? "修改" : "新增");
        return d;
    }

    /**
     * 获取点位的旧设备List信息
     *
     * @param context
     * @return
     */
    public static List<DDeviceOldEntity> getDDeviceCcOldEntities(Context context) {
        return GreenDaoManager.getInstance().getmDaoSession().getDDeviceOldEntityDao().queryBuilder()
                .where(DDeviceOldEntityDao.Properties.PGuid.eq(SPUtils.get(context, "project_guid", ""))
                        , DDeviceOldEntityDao.Properties.PPGuid.eq(SPUtils.get(context, "UUID", ""))
                        , DDeviceOldEntityDao.Properties.DRWork.eq("拆除")
                        , DDeviceOldEntityDao.Properties.DRDatastate.notEq("删除")).list();
    }

    /**
     * 获取点位的旧设备List信息
     *
     * @param context
     * @return
     */
    public static List<DDeviceOldEntity> getDDeviceXlOldEntities(Context context) {
        return GreenDaoManager.getInstance().getmDaoSession().getDDeviceOldEntityDao().queryBuilder()
                .where(DDeviceOldEntityDao.Properties.PGuid.eq(SPUtils.get(context, "project_guid", ""))
                        , DDeviceOldEntityDao.Properties.PPGuid.eq(SPUtils.get(context, "UUID", ""))
                        , DDeviceOldEntityDao.Properties.DRWork.eq("修理")
                        , DDeviceOldEntityDao.Properties.DRDatastate.notEq("删除")).list();
    }

    /**
     * 获取点位的旧设备信息
     *
     * @param context
     * @return
     */
    @Nullable
    public static DDeviceOldEntity getDDeviceOldEntity(Context context) {
        List<DDeviceOldEntity> list = GreenDaoManager.getInstance().getmDaoSession().getDDeviceOldEntityDao().queryBuilder()
                .where(DDeviceOldEntityDao.Properties.PGuid.eq(SPUtils.get(context, "project_guid", ""))
                        , DDeviceOldEntityDao.Properties.PPGuid.eq(SPUtils.get(context, "UUID", ""))
                        , DDeviceOldEntityDao.Properties.DRDatastate.notEq("删除")).list();
        if (list.size() > 0)
            return list.get(0);
        return null;
    }

    /**
     * 获取点位的旧设备信息
     *
     * @param context
     * @return
     */
    @Nullable
    public static EDeviceOldEntity getEDeviceOldEntity(Context context) {
        List<EDeviceOldEntity> list = GreenDaoManager.getInstance().getmDaoSession().getEDeviceOldEntityDao().queryBuilder()
                .where(EDeviceOldEntityDao.Properties.PGuid.eq(SPUtils.get(context, "project_guid", ""))
                        , EDeviceOldEntityDao.Properties.PPGuid.eq(SPUtils.get(context, "UUID", ""))
                        , EDeviceOldEntityDao.Properties.DRDatastate.notEq("删除")).list();
        if (list.size() > 0)
            return list.get(0);
        return null;
    }

    /**
     * 获取点位的新设备List信息
     *
     * @param context
     * @return
     */
    public static List<DDeviceBuyEntity> getDDeviceBuyEntities(Context context) {
        return GreenDaoManager.getInstance().getmDaoSession().getDDeviceBuyEntityDao().queryBuilder()
                .where(DDeviceBuyEntityDao.Properties.PGuid.eq(SPUtils.get(context, "project_guid", ""))
                        , DDeviceBuyEntityDao.Properties.PPGuid.eq(SPUtils.get(context, "UUID", ""))
                        , DDeviceBuyEntityDao.Properties.DBDatastate.notEq("删除")).list();
    }

    /**
     * 获取点位的新材料List信息
     *
     * @param context
     * @return
     */
    public static List<DMaterialBuyEntity> getDMaterialBuyEntities(Context context) {
        return GreenDaoManager.getInstance().getmDaoSession().getDMaterialBuyEntityDao().queryBuilder()
                .where(DMaterialBuyEntityDao.Properties.PGuid.eq(SPUtils.get(context, "project_guid", ""))
                        , DMaterialBuyEntityDao.Properties.PPGuid.eq(SPUtils.get(context, "UUID", ""))
                        , DMaterialBuyEntityDao.Properties.MBDatastate.notEq("删除")).list();
    }

    /**
     * 获取点位的新材料List信息
     *
     * @param context
     * @return
     */
    public static List<EMaterialBuyEntity> getEMaterialBuyEntities(Context context) {
        return GreenDaoManager.getInstance().getmDaoSession().getEMaterialBuyEntityDao().queryBuilder()
                .where(EMaterialBuyEntityDao.Properties.PGuid.eq(SPUtils.get(context, "project_ys_guid", ""))
                        , EMaterialBuyEntityDao.Properties.PPGuid.eq(SPUtils.get(context, "UUID", ""))
                        , EMaterialBuyEntityDao.Properties.MBDatastate.notEq("删除")).list();
    }

    /**
     * 获取点位的新材料List信息
     *
     * @param context
     * @return
     */
    public static List<DOtherCostEntity> getDOtherCostEntities(Context context) {
        return GreenDaoManager.getInstance().getmDaoSession().getDOtherCostEntityDao().queryBuilder()
                .where(DOtherCostEntityDao.Properties.PGuid.eq(SPUtils.get(context, "project_guid", ""))
                        , DOtherCostEntityDao.Properties.PPGuid.eq(SPUtils.get(context, "UUID", ""))
                        , DOtherCostEntityDao.Properties.CCDatastate.notEq("删除")).list();
    }

    /**
     * 获取点位的旧材料List信息
     *
     * @param context
     * @return
     */
    public static List<DMaterialOldEntity> getDMaterialOldEntities(Context context, String process) {
        return GreenDaoManager.getInstance().getmDaoSession().getDMaterialOldEntityDao().queryBuilder()
                .where(DMaterialOldEntityDao.Properties.PGuid.eq(SPUtils.get(context, "project_guid", ""))
                        , DMaterialOldEntityDao.Properties.PPGuid.eq(SPUtils.get(context, "UUID", ""))
                        , DMaterialOldEntityDao.Properties.MBWork.eq(process)
                        , DMaterialOldEntityDao.Properties.MBDatastate.notEq("删除")).list();
    }

    /**
     * 获取点位的旧材料List信息
     *
     * @param context
     * @return
     */
    public static List<EMaterialOldEntity> getEMaterialOldEntities(Context context, String process) {
        return GreenDaoManager.getInstance().getmDaoSession().getEMaterialOldEntityDao().queryBuilder()
                .where(EMaterialOldEntityDao.Properties.PGuid.eq(SPUtils.get(context, "project_ys_guid", ""))
                        , EMaterialOldEntityDao.Properties.PPGuid.eq(SPUtils.get(context, "UUID", ""))
                        , EMaterialOldEntityDao.Properties.MBWork.eq(process)
                        , EMaterialOldEntityDao.Properties.MBDatastate.notEq("删除")).list();
    }

    /**
     * 获取点位的新设备信息
     *
     * @param context
     * @return
     */
    @Nullable
    public static DDeviceBuyEntity getDDeviceBuyEntity(Context context) {
        List<DDeviceBuyEntity> list = GreenDaoManager.getInstance().getmDaoSession().getDDeviceBuyEntityDao().queryBuilder()
                .where(DDeviceBuyEntityDao.Properties.PGuid.eq(SPUtils.get(context, "project_guid", ""))
                        , DDeviceBuyEntityDao.Properties.PPGuid.eq(SPUtils.get(context, "UUID", ""))
                        , DDeviceBuyEntityDao.Properties.DBDatastate.notEq("删除")).list();
        if (list.size() > 0)
            return list.get(0);
        return null;
    }

    /**
     * 获取点位的新设备信息
     *
     * @param context
     * @return
     */
    @Nullable
    public static EDeviceBuyEntity getEDeviceBuyEntity(Context context) {
        List<EDeviceBuyEntity> list = GreenDaoManager.getInstance().getmDaoSession().getEDeviceBuyEntityDao().queryBuilder()
                .where(EDeviceBuyEntityDao.Properties.PGuid.eq(SPUtils.get(context, "project_ys_guid", ""))
                        , EDeviceBuyEntityDao.Properties.PPGuid.eq(SPUtils.get(context, "UUID", ""))
                        , EDeviceBuyEntityDao.Properties.DBDatastate.notEq("删除")).list();
        if (list.size() > 0)
            return list.get(0);
        return null;
    }

    /**
     * 添加设备
     *
     * @param fragment
     * @param requestCode
     */
    public static void addDevice(final Fragment fragment, final String process, final int requestCode) {

        List<String> bs = new ArrayList<>();
        bs.add("新设备");
        bs.add("搜索");
        bs.add("本项目利旧");
        bs.add("复制点位设备");
        new BottomClickDialog().setDatas(bs).setListener(new ResponseListener() {
            @Override
            public void response(Object... data) {
                switch ((String) data[0]) {
                    case "新设备":
                        DaoSession session = GreenDaoManager.getInstance().getmDaoSession();
                        String pointName = (String) SPUtils.get(fragment.getActivity(), "point_name", "");
                        final BPointConfigEntity b = session.getBPointConfigEntityDao().queryBuilder().where(BPointConfigEntityDao.Properties.PCPoint.eq(pointName)
                                , BPointConfigEntityDao.Properties.PCProcess.eq(process), BPointConfigEntityDao.Properties.PCType.eq(0)).unique();
                        if (b == null) {
                            T.showLong(fragment.getActivity(), "此操作还未配置点位信息，请联系管理员");
                            return;
                        }
                        List<String> labels = new ArrayList<>();
                        if (TextUtils.isEmpty(b.getPCLabel())) {
                            labels.add(pointName);
                            Intent intent = new Intent(fragment.getActivity(), AddDeviceActivity.class);
                            intent.putExtra("label", pointName);
                            fragment.startActivityForResult(intent, requestCode);
                        } else {
                            labels.addAll(Arrays.asList(b.getPCLabel().split(",")));
                        }
                        new ListDialog<String>().setDatas(labels).setTitle("请选择设备类型").setListener(new ResponseListener() {
                            @Override
                            public void response(Object... data) {
                                String label = data[0].toString();
                                Intent intent = new Intent(fragment.getActivity(), AddDeviceActivity.class);
                                intent.putExtra("label", label);
                                fragment.startActivityForResult(intent, requestCode);
                            }
                        }).show(fragment.getFragmentManager(), "");
                        break;
                    case "搜索":
                        Intent intent = new Intent(fragment.getActivity(), SearchDeviceActivity.class);
                        fragment.startActivityForResult(intent, requestCode);
                        break;
                    case "本项目利旧":
                        if (fragment instanceof YsLineDeviceFragment) {
                            Intent intent1 = new Intent(fragment.getActivity(), YsUserOldDeviceActivity.class);
                            fragment.startActivityForResult(intent1, requestCode);
                        } else {
                            Intent intent2 = new Intent(fragment.getActivity(), UserOldDeviceActivity.class);
                            fragment.startActivityForResult(intent2, requestCode);
                        }
                        break;
                    case "复制点位设备":
                        Double a = Double.parseDouble((String) SPUtils.get(fragment.getActivity(), "drag_lat", ""));
                        Double g = Double.parseDouble((String) SPUtils.get(fragment.getActivity(), "drag_lgt", ""));
                        Intent intent3 = null;
                        if (fragment instanceof YsLineDeviceFragment) {
                            intent3 = new Intent(fragment.getActivity(), ChoiceYsPointActivity.class);
                        } else {
                            intent3 = new Intent(fragment.getActivity(), ChoicePointActivity.class);
                        }
                        intent3.putExtra("lat", a);
                        intent3.putExtra("lgt", g);
                        fragment.startActivityForResult(intent3, 4);
                        break;
                }
            }
        }).show(fragment.getChildFragmentManager(), "");
//        DaoSession session = GreenDaoManager.getInstance().getmDaoSession();
//        String pointName = (String) SPUtils.get(fragment.getActivity(), "point_name", "");
//        final BPointConfigEntity b = session.getBPointConfigEntityDao().queryBuilder().where(BPointConfigEntityDao.Properties.PCPoint.eq(pointName)
//                , BPointConfigEntityDao.Properties.PCProcess.eq(process), BPointConfigEntityDao.Properties.PCType.eq(0)).unique();
//        if (b == null) {
//            T.showLong(fragment.getActivity(), "此操作还未配置点位信息，请联系管理员");
//            return;
//        }
//        List<String> labels = new ArrayList<>();
//        labels.add("搜索");
//        if (process.equals("新增")) {
//            labels.add("本项目利旧");
//        }
//        if (TextUtils.isEmpty(b.getPCLabel())) {
//            labels.add(pointName);
//            Intent intent = new Intent(fragment.getActivity(), AddDeviceActivity.class);
//            intent.putExtra("label", pointName);
//            fragment.startActivityForResult(intent, requestCode);
//        } else {
//            labels.addAll(Arrays.asList(b.getPCLabel().split(",")));
//        }
//        new ListDialog<String>().setDatas(labels).setTitle("请选择设备类型").setListener(new ResponseListener() {
//            @Override
//            public void response(Object... data) {
//                String label = data[0].toString();
//                if (label.equals("搜索")) {
//                    Intent intent = new Intent(fragment.getActivity(), SearchDeviceActivity.class);
//                    fragment.startActivityForResult(intent, requestCode);
//                } else if (label.equals("本项目利旧")) {
//                    if (fragment instanceof YsLineDeviceFragment) {
//                        Intent intent = new Intent(fragment.getActivity(), YsUserOldDeviceActivity.class);
//                        fragment.startActivityForResult(intent, requestCode);
//                    } else {
//                        Intent intent = new Intent(fragment.getActivity(), UserOldDeviceActivity.class);
//                        fragment.startActivityForResult(intent, requestCode);
//                    }
//                } else {
//                    Intent intent = new Intent(fragment.getActivity(), AddDeviceActivity.class);
//                    intent.putExtra("label", label);
//                    fragment.startActivityForResult(intent, requestCode);
//                }
//            }
//        }).show(fragment.getFragmentManager(), "");
    }

    /**
     * 编辑旧设备
     *
     * @param fragment
     * @param d
     * @param requestCode
     */
    public static void editOldDevice(Fragment fragment, int index, DDeviceOldEntity d, int requestCode) {
        Intent intent = new Intent(fragment.getActivity(), EditePointDeviceActivity.class);
        intent.putExtra("device", d);
        intent.putExtra("index", index);
        fragment.startActivityForResult(intent, requestCode);
    }

    /**
     * 编辑旧设备
     *
     * @param fragment
     * @param d
     * @param requestCode
     */
    public static void editYsOldDevice(Fragment fragment, int index, EDeviceOldEntity d, int requestCode) {
        Intent intent = new Intent(fragment.getActivity(), YsEditePointDeviceActivity.class);
        intent.putExtra("device", d);
        intent.putExtra("index", index);
        fragment.startActivityForResult(intent, requestCode);
    }

    /**
     * 编辑购买设备
     *
     * @param fragment
     */
    public static void editBuyDevice(Fragment fragment, int index, DDeviceBuyEntity d, int requestCode) {
        Intent intent = new Intent(fragment.getActivity(), EditDeviceActivity.class);
        intent.putExtra("device", d);
        intent.putExtra("index", index);
        fragment.startActivityForResult(intent, requestCode);
    }

    /**
     * 编辑购买设备
     *
     * @param fragment
     */
    public static void editYsBuyDevice(Fragment fragment, int index, EDeviceBuyEntity d, int requestCode) {
        Intent intent = new Intent(fragment.getActivity(), YsEditDeviceActivity.class);
        intent.putExtra("device", d);
        intent.putExtra("index", index);
        fragment.startActivityForResult(intent, requestCode);
    }

    /**
     * 添加材料
     *
     * @param fragment
     * @param requestCode
     */
    public static void addMaterial(final Fragment fragment, final String process, final int requestCode) {
        List<String> bs = new ArrayList<>();
        bs.add("新材料");
        bs.add("模块");
        bs.add("搜索");
        bs.add("本项目利旧");
        bs.add("复制点位材料");
        new BottomClickDialog().setDatas(bs).setListener(new ResponseListener() {
            @Override
            public void response(Object... data) {
                switch ((String) data[0]) {
                    case "新材料":
                        DaoSession session = GreenDaoManager.getInstance().getmDaoSession();
                        String pointName = (String) SPUtils.get(fragment.getActivity(), "point_name", "");
                        final BPointConfigEntity b = session.getBPointConfigEntityDao().queryBuilder().where(BPointConfigEntityDao.Properties.PCPoint.eq(pointName)
                                , BPointConfigEntityDao.Properties.PCProcess.eq(process), BPointConfigEntityDao.Properties.PCType.eq(1)).unique();
                        if (b == null) {
                            T.showLong(fragment.getActivity(), "此操作还未配置点位信息，请联系管理员");
                            return;
                        }
                        List<String> labels = new ArrayList<>();
                        if (TextUtils.isEmpty(b.getPCLabel())) {
                            labels.add(pointName);
                        } else {
                            labels.addAll(Arrays.asList(b.getPCLabel().split(",")));
                        }
                        new ListDialog<String>().setDatas(labels).setTitle("请选择材料类型").setListener(new ResponseListener() {
                            @Override
                            public void response(Object... data) {
                                String label = data[0].toString();
                                Intent intent = new Intent(fragment.getActivity(), AddMaterialActivity.class);
                                intent.putExtra("label", label);
                                fragment.startActivityForResult(intent, requestCode);
                            }
                        }).show(fragment.getFragmentManager(), "");
                        break;
                    case "搜索":
                        Intent intent = new Intent(fragment.getActivity(), SearchMaterialActivity.class);
                        fragment.startActivityForResult(intent, requestCode);
                        break;
                    case "本项目利旧":
                        if (fragment instanceof YsMaterialFragment) {
                            Intent intent2 = new Intent(fragment.getActivity(), YsUserOldMaterialActivity.class);
                            fragment.startActivityForResult(intent2, requestCode);
                        } else {
                            Intent intent3 = new Intent(fragment.getActivity(), UserOldMaterialActivity.class);
                            fragment.startActivityForResult(intent3, requestCode);
                        }
                        break;
                    case "复制点位材料":
                        Double a = Double.parseDouble((String) SPUtils.get(fragment.getActivity(), "drag_lat", ""));
                        Double g = Double.parseDouble((String) SPUtils.get(fragment.getActivity(), "drag_lgt", ""));
                        Intent intent3 = null;
                        if (fragment instanceof YsLineDeviceFragment) {
                            intent3 = new Intent(fragment.getActivity(), ChoiceYsPointActivity.class);
                        } else {
                            intent3 = new Intent(fragment.getActivity(), ChoicePointActivity.class);
                        }
                        intent3.putExtra("lat", a);
                        intent3.putExtra("lgt", g);
                        fragment.startActivityForResult(intent3, 4);
                        break;
                    case "模块":
                        Intent intent1 = new Intent(fragment.getActivity(), MaterialModelActivity.class);
                        intent1.putExtra("choice", true);
                        fragment.startActivityForResult(intent1, requestCode);
                        break;
                }
            }
        }).show(fragment.getChildFragmentManager(), "");
    }

    /**
     * 添加材料
     *
     * @param requestCode
     */
    public static void addMaterial(final Activity activity, final int requestCode) {
        List<String> bs = new ArrayList<>();
        bs.add("新材料");
        bs.add("搜索");
        new BottomClickDialog().setDatas(bs).setListener(new ResponseListener() {
            @Override
            public void response(Object... data) {
                switch ((String) data[0]) {
                    case "新材料":
                        DaoSession session = GreenDaoManager.getInstance().getmDaoSession();
                        final BPointConfigEntity b = session.getBPointConfigEntityDao().queryBuilder()
                                .where(BPointConfigEntityDao.Properties.PCPoint.eq("模块")
                                        , BPointConfigEntityDao.Properties.PCType.eq(1)).unique();
                        if (b == null) {
                            T.showLong(activity, "此操作还未配置信息，请联系管理员");
                            return;
                        }
                        List<String> labels = new ArrayList<>();
                        if (TextUtils.isEmpty(b.getPCLabel())) {
                            labels.add("模块");
                        } else {
                            labels.addAll(Arrays.asList(b.getPCLabel().split(",")));
                        }
                        new ListDialog<String>().setDatas(labels).setTitle("请选择材料类型").setListener(new ResponseListener() {
                            @Override
                            public void response(Object... data) {
                                String label = data[0].toString();
                                Intent intent = new Intent(activity, AddMaterialActivity.class);
                                intent.putExtra("label", label);
                                activity.startActivityForResult(intent, requestCode);
                            }
                        }).show(activity.getFragmentManager(), "");
                        break;
                    case "搜索":
                        Intent intent = new Intent(activity, SearchMaterialActivity.class);
                        activity.startActivityForResult(intent, requestCode);
                        break;
                }
            }
        }).show(activity.getFragmentManager(), "");
    }

    /**
     * 添加问题
     *
     * @param fragment
     * @param requestCode
     */
    public static void addProblem(final Fragment fragment, final int requestCode) {
        DaoSession session = GreenDaoManager.getInstance().getmDaoSession();
        String pointName = (String) SPUtils.get(fragment.getActivity(), "point_name", "");
        final BPointConfigEntity b = session.getBPointConfigEntityDao().queryBuilder().where(BPointConfigEntityDao.Properties.PCPoint.eq(pointName)
                , BPointConfigEntityDao.Properties.PCType.eq(99)).unique();
        if (b == null) {
            T.showLong(fragment.getActivity(), "此操作还未配置点位信息，请联系管理员");
            return;
        }
        if (TextUtils.isEmpty(b.getPCLabel())) {
            Intent intent = new Intent(fragment.getActivity(), AddProblemActivity.class);
            intent.putExtra("label", pointName);
            fragment.startActivityForResult(intent, requestCode);
        } else {
            List<String> labels = Arrays.asList(b.getPCLabel().split(","));
            new ListDialog<String>().setDatas(labels).setTitle("请选择问题类型").setListener(new ResponseListener() {
                @Override
                public void response(Object... data) {
                    Intent intent = new Intent(fragment.getActivity(), AddProblemActivity.class);
                    intent.putExtra("label", data[0].toString());
                    fragment.startActivityForResult(intent, requestCode);
                }
            }).show(fragment.getFragmentManager(), "");
        }
    }

    /**
     * 添加其他工程量
     *
     * @param fragment
     * @param requestCode
     */
    public static void addOtherQuantity(final Fragment fragment, final int requestCode) {
        List<String> bs = new ArrayList<>();
        bs.add("新工程量");
        bs.add("复制点位工程量");
        new BottomClickDialog().setDatas(bs).setListener(new ResponseListener() {
            @Override
            public void response(Object... data) {
                switch ((String) data[0]) {
                    case "新工程量":
                        String pointName = SPUtils.get(fragment.getActivity(), "point_name", "").toString();
                        List<String> strings = XUtils.distinctQuery(BOtherQuantitiesEntityDao.Properties.OQSubclass.columnName
                                , BOtherQuantitiesEntityDao.TABLENAME, BOtherQuantitiesEntityDao.Properties.OQPoint.columnName + " = '" + pointName + "'");
                        if (strings.size() == 0) {
                            T.showLong(fragment.getActivity(), "该点位暂未录入其他工程量");
                            return;
                        }
                        if (strings.size() == 1) {
                            Intent intent = new Intent(fragment.getActivity(), AddQuantitiesActivity.class);
                            intent.putExtra("label", strings.get(0));
                            fragment.startActivityForResult(intent, requestCode);
                        } else
                            new ListDialog<String>().setDatas(strings).setListener(new ResponseListener() {
                                @Override
                                public void response(Object... data) {
                                    String s = (String) data[0];
                                    Intent intent = new Intent(fragment.getActivity(), AddQuantitiesActivity.class);
                                    intent.putExtra("label", s);
                                    fragment.startActivityForResult(intent, requestCode);
                                }
                            }).show(fragment.getFragmentManager(), "");
                        break;
                    case "复制点位工程量":
                        Double a = Double.parseDouble((String) SPUtils.get(fragment.getActivity(), "drag_lat", ""));
                        Double g = Double.parseDouble((String) SPUtils.get(fragment.getActivity(), "drag_lgt", ""));
                        Intent intent3 = null;
                        if (fragment instanceof YsLineDeviceFragment) {
                            intent3 = new Intent(fragment.getActivity(), ChoiceYsPointActivity.class);
                        } else {
                            intent3 = new Intent(fragment.getActivity(), ChoicePointActivity.class);
                        }
                        intent3.putExtra("lat", a);
                        intent3.putExtra("lgt", g);
                        fragment.startActivityForResult(intent3, 4);
                        break;
                }
            }
        }).show(fragment.getChildFragmentManager(), "");
    }


    /**
     * 编辑材料
     *
     * @param fragment
     * @param requestCode
     */
    public static void editBuyMaterial(Fragment fragment, DMaterialBuyEntity d, int index, int requestCode) {
        Intent intent = new Intent(fragment.getActivity(), EditMaterialActivity.class);
        intent.putExtra("material", d);
        intent.putExtra("index", index);
        fragment.startActivityForResult(intent, requestCode);
    }

    /**
     * 编辑材料
     *
     * @param fragment
     * @param requestCode
     */
    public static void editYsBuyMaterial(Fragment fragment, EMaterialBuyEntity d, int index, int requestCode) {
        Intent intent = new Intent(fragment.getActivity(), YsEditMaterialActivity.class);
        intent.putExtra("material", d);
        intent.putExtra("index", index);
        fragment.startActivityForResult(intent, requestCode);
    }

    /**
     * 编辑材料
     *
     * @param fragment
     * @param requestCode
     */
    public static void editOldMaterial(Fragment fragment, DMaterialOldEntity d, int index, int requestCode) {
        Intent intent = new Intent(fragment.getActivity(), EditOldMaterialActivity.class);
        intent.putExtra("material", d);
        intent.putExtra("index", index);
        fragment.startActivityForResult(intent, requestCode);
    }

    /**
     * 编辑材料
     *
     * @param fragment
     * @param requestCode
     */
    public static void editYsOldMaterial(Fragment fragment, EMaterialOldEntity d, int index, int requestCode) {
        Intent intent = new Intent(fragment.getActivity(), YsEditOldMaterialActivity.class);
        intent.putExtra("material", d);
        intent.putExtra("index", index);
        fragment.startActivityForResult(intent, requestCode);
    }

    /**
     * 添加购买的材料
     *
     * @param context
     * @param b
     * @return
     */
    public static DMaterialBuyEntity addBuyMaterial(Context context, BMaterialEntity b) {
        DMaterialBuyEntity d = new DMaterialBuyEntity();
        d.setGuid(UUID.randomUUID().toString());
        d.setPGuid((String) SPUtils.get(context, "project_guid", ""));
        d.setPPGuid((String) SPUtils.get(context, "UUID", ""));
        d.setMMGuid(b.getGuid());
        d.setMBBigtype(b.getMBigtype());
        d.setMBMidtype(b.getMMidtype());
        d.setMBName(b.getMName());
        d.setDLabel(b.getMLabel());
        d.setMBParameter(b.getMParameter());
        d.setMBWeight(b.getMWeight());
        d.setMBUnit(b.getMUnit());
        d.setMBCount(b.getmCount());
        d.setMBPrice(b.getMPrice());
        d.setMBPriceall((b.getMPrice() == null ? 0 : Math.round(b.getMPrice()) * b.getmCount() * 1000) / 1000);
        d.setMBWeightall(d.getMBCount() * (d.getMBWeight() == null ? 0 : d.getMBWeight()));
        d.setMBSource("人工");
        d.setMBDatastate("新增");
        return d;
    }

    /**
     * 添加购买的材料
     *
     * @param context
     * @param b
     * @return
     */
    public static EMaterialBuyEntity addYsBuyMaterial(Context context, BMaterialEntity b) {
        EMaterialBuyEntity d = new EMaterialBuyEntity();
        d.setGuid(UUID.randomUUID().toString());
        d.setPGuid((String) SPUtils.get(context, "project_ys_guid", ""));
        d.setPPGuid((String) SPUtils.get(context, "UUID", ""));
        d.setMMGuid(b.getGuid());
        d.setMBBigtype(b.getMBigtype());
        d.setMBMidtype(b.getMMidtype());
        d.setMBName(b.getMName());
        d.setDLabel(b.getMLabel());
        d.setMBParameter(b.getMParameter());
        d.setMBWeight(b.getMWeight());
        d.setMBUnit(b.getMUnit());
        d.setMBCount(b.getmCount());
        d.setMBPrice(b.getMPrice());
        d.setMBPriceall((b.getMPrice() == null ? 0 : b.getMPrice()) * b.getmCount());
        d.setMBWeightall(d.getMBCount() * (d.getMBWeight() == null ? 0 : d.getMBWeight()));
        d.setMBSource("人工");
        d.setMBDatastate("新增");
        return d;
    }

    /**
     * 添加旧材料
     *
     * @param context
     * @param b
     * @return
     */
    public static DMaterialOldEntity addOldMaterial(Context context, BMaterialEntity b, String process) {
        DMaterialOldEntity d = new DMaterialOldEntity();
        d.setGuid(UUID.randomUUID().toString());
        d.setPGuid((String) SPUtils.get(context, "project_guid", ""));
        d.setPPGuid((String) SPUtils.get(context, "UUID", ""));
        d.setMMGuid(b.getGuid());
        d.setMBBigtype(b.getMBigtype());
        d.setMBMidtype(b.getMMidtype());
        d.setMBName(b.getMName());
        d.setDLabel(b.getMLabel());
        d.setMBParameter(b.getMParameter());
        d.setMBWeight(b.getMWeight());
        d.setMBUnit(b.getMUnit());
        d.setMBCount(b.getmCount());
        d.setDRWhere(b.getmCount() + "");
        String sp = (String) SPUtils.get(context, "opinion_type", "");
        d.setDROpinion(sp.equals("") ? DBUtils.getBDataDictionaryEntities("opinion_type").get(0).getDKey() : sp);
        d.setMBWork(process);
        d.setMBDatastate("新增");
        return d;
    }

    /**
     * 添加旧材料
     *
     * @param context
     * @param b
     * @return
     */
    public static EMaterialOldEntity addYsOldMaterial(Context context, BMaterialEntity b, String process) {
        EMaterialOldEntity d = new EMaterialOldEntity();
        d.setGuid(UUID.randomUUID().toString());
        d.setPGuid((String) SPUtils.get(context, "project_ys_guid", ""));
        d.setPPGuid((String) SPUtils.get(context, "UUID", ""));
        d.setMMGuid(b.getGuid());
        d.setMBBigtype(b.getMBigtype());
        d.setMBMidtype(b.getMMidtype());
        d.setMBName(b.getMName());
        d.setDLabel(b.getMLabel());
        d.setMBParameter(b.getMParameter());
        d.setMBWeight(b.getMWeight());
        d.setMBUnit(b.getMUnit());
        d.setMBCount(b.getmCount());
        d.setDRWhere(b.getmCount() + "");
        String sp = (String) SPUtils.get(context, "opinion_type", "");
        d.setDROpinion(sp.equals("") ? DBUtils.getBDataDictionaryEntities("opinion_type").get(0).getDKey() : sp);
        d.setMBWork(process);
        d.setMBDatastate("新增");
        return d;
    }

    public static DOtherCostEntity addOtherCost(Context context, BOtherQuantitiesEntity b) {
        DOtherCostEntity d = new DOtherCostEntity();
        d.setGuid(UUID.randomUUID().toString());
        d.setPGuid((String) SPUtils.get(context, "project_guid", ""));
        d.setPPGuid((String) SPUtils.get(context, "UUID", ""));
        d.setOCWork(b.getOQWork());
        d.setOCSubclass(b.getOQSubclass());
        d.setOCName(b.getOQName());
        d.setOCParametertype(b.getOQParametertype());
        d.setOCParameter(b.getOQParameter());
        d.setOCMeterUnit(b.getOQMeterUnit());
        d.setOCPrice(b.getOQPrice());
        d.setOCPriceall(b.getOQPrice() == null ? 0 : (double) Math.round(b.getOQPrice() * b.getCount() * 100) / 100);
        d.setOQCount(b.getCount());
        d.setOCRemarks(b.getOQRemarks());
        d.setCCDatastate("新增");
        return d;
    }

    /**
     * 删除交叉跨越
     *
     * @param context
     */
    public static void deleteCross(Activity context) {
        DaoSession session = GreenDaoManager.getInstance().getmDaoSession();
        String uuid = (String) SPUtils.get(context, "UUID", "");
        DCrossCostEntity dCrossCostEntity = session.getDCrossCostEntityDao().load(uuid);
        dCrossCostEntity.setCCDatastate("删除");
        session.getDCrossCostEntityDao().update(dCrossCostEntity);//删除交叉跨越
        List<DPictureEntity> dPictureEntities = session.getDPictureEntityDao().queryBuilder().where(DPictureEntityDao.Properties.PPGuid.eq(uuid)
                , DPictureEntityDao.Properties.PDatastate.notEq("删除")
                , DPictureEntityDao.Properties.PGuid.eq((String) SPUtils.get(context, "project_guid", ""))).list();
        for (DPictureEntity d : dPictureEntities) {
            d.setPDatastate("删除");
        }
        session.getDPictureEntityDao().updateInTx(dPictureEntities);//删除点位照片
        context.finish();
    }

    public static void deleteYsCross(Activity context) {
        DaoSession session = GreenDaoManager.getInstance().getmDaoSession();
        String uuid = (String) SPUtils.get(context, "UUID", "");
        ECrossCostEntity dCrossCostEntity = session.getECrossCostEntityDao().load(uuid);
        dCrossCostEntity.setCCDatastate("删除");
        session.getECrossCostEntityDao().update(dCrossCostEntity);//删除交叉跨越
        List<DPictureEntity> dPictureEntities = session.getDPictureEntityDao().queryBuilder().where(DPictureEntityDao.Properties.PPGuid.eq(uuid)
                , DPictureEntityDao.Properties.PDatastate.notEq("删除")
                , DPictureEntityDao.Properties.PGuid.eq((String) SPUtils.get(context, "project_ys_guid", ""))).list();
        for (DPictureEntity d : dPictureEntities) {
            d.setPDatastate("删除");
        }
        session.getDPictureEntityDao().updateInTx(dPictureEntities);//删除点位照片
        List<EProblemexistEntity> eProblemexistEntities = session.getEProblemexistEntityDao().queryBuilder().where(EProblemexistEntityDao.Properties.Pointguid.eq(uuid)
                , EProblemexistEntityDao.Properties.Datastate.notEq("删除")
                , EProblemexistEntityDao.Properties.Projectguid.eq((String) SPUtils.get(context, "project_ys_guid", ""))).list();
        for (EProblemexistEntity d : eProblemexistEntities) {
            d.setDatastate("删除");
        }
        session.getEProblemexistEntityDao().updateInTx(eProblemexistEntities);
        context.finish();
    }

    /**
     * 删除点位
     *
     * @param context
     */
    public static void deletePoint(Activity context) {
        DaoSession session = GreenDaoManager.getInstance().getmDaoSession();
        String uuid = (String) SPUtils.get(context, "UUID", "");
        DPointEntity pointEntity = session.getDPointEntityDao().load(uuid);
        long upCount = session.getDPointEntityDao().queryBuilder()
                .where(DPointEntityDao.Properties.PPUpguid.eq(uuid)
                        , DPointEntityDao.Properties.PGuid.eq((String) SPUtils.get(context, "project_guid", ""))
                        , DPointEntityDao.Properties.PPDatastate.notEq("删除")).count();
        if (upCount > 0) {
            T.showLong(context, "该点位有下联点位，请先删除下联点位后再删除该点位");
            return;
        }
        long paCount = session.getDPointEntityDao().queryBuilder()
                .where(DPointEntityDao.Properties.PPParentguid.eq(uuid)
                        , DPointEntityDao.Properties.PGuid.eq((String) SPUtils.get(context, "project_guid", ""))
                        , DPointEntityDao.Properties.PPDatastate.notEq("删除")).count();
        if (paCount > 0) {
            T.showLong(context, "该点位有子点位，请先删除子点位后再删除该点位");
            return;
        }

        List<DDeviceOldEntity> dDeviceOldEntities = session.getDDeviceOldEntityDao().queryBuilder().where(
                DDeviceOldEntityDao.Properties.PPGuid.eq(uuid)
                , DDeviceOldEntityDao.Properties.DRDatastate.notEq("删除")
                , DDeviceOldEntityDao.Properties.PGuid.eq((String) SPUtils.get(context, "project_guid", ""))).list();
        for (DDeviceOldEntity d : dDeviceOldEntities) {//工程量后面统一删除
            d.setDRDatastate("删除");
            if (d.getDRWork().equals("拆除") && d.getDROpinion().equals("-9")) {
                T.showLong(context, "该点位拆除设备已被利旧，请先删除利旧设备");
                return;
            }
        }
        List<DMaterialOldEntity> dMaterialOldEntities = session.getDMaterialOldEntityDao().queryBuilder().where(
                DMaterialOldEntityDao.Properties.PPGuid.eq(uuid)
                , DMaterialOldEntityDao.Properties.MBDatastate.notEq("删除")
                , DMaterialOldEntityDao.Properties.PGuid.eq((String) SPUtils.get(context, "project_guid", ""))).list();
        for (DMaterialOldEntity d : dMaterialOldEntities) {//工程量后面统一删除
            d.setMBDatastate("删除");
            if (d.getDROpinion().equals("10") && d.getMBCount() > Double.parseDouble(d.getDRWhere())) {
                T.showLong(context, "该点位有拆除材料已被利旧，请先删除利旧材料");
                return;
            }
        }
        session.getDMaterialOldEntityDao().updateInTx(dMaterialOldEntities);//删除旧材料
        session.getDDeviceOldEntityDao().updateInTx(dDeviceOldEntities);//删除旧设备


        pointEntity.setPPDatastate("删除");
        session.getDPointEntityDao().update(pointEntity);//删除点位
        List<DPictureEntity> dPictureEntities = session.getDPictureEntityDao().queryBuilder().where(DPictureEntityDao.Properties.PPGuid.eq(uuid)
                , DPictureEntityDao.Properties.PDatastate.notEq("删除")
                , DPictureEntityDao.Properties.PGuid.eq((String) SPUtils.get(context, "project_guid", ""))).list();
        for (DPictureEntity d : dPictureEntities) {
            d.setPDatastate("删除");
        }
        session.getDPictureEntityDao().updateInTx(dPictureEntities);//删除点位照片

        List<DDeviceBuyEntity> dDeviceBuyEntities = session.getDDeviceBuyEntityDao().queryBuilder().where(
                DDeviceBuyEntityDao.Properties.PPGuid.eq(uuid)
                , DDeviceBuyEntityDao.Properties.DBDatastate.notEq("删除")
                , DDeviceBuyEntityDao.Properties.PGuid.eq((String) SPUtils.get(context, "project_guid", ""))).list();
        for (DDeviceBuyEntity d : dDeviceBuyEntities) {//工程量后面统一删除
            d.setDBDatastate("删除");
            if (d.getDBUnit().equals("利旧")) {//如果是利旧设备则将原来的利旧设备修改为未使用状态
                DDeviceOldEntity load = session.getDDeviceOldEntityDao().load(d.getDDGuid());
                load.setDROpinion("10");
                session.getDDeviceOldEntityDao().update(load);
            }
        }
        session.getDDeviceBuyEntityDao().updateInTx(dDeviceBuyEntities);//删除新购设备

        List<DMaterialBuyEntity> dMaterialBuyEntities = session.getDMaterialBuyEntityDao().queryBuilder().where(
                DMaterialBuyEntityDao.Properties.PPGuid.eq(uuid)
                , DMaterialBuyEntityDao.Properties.MBDatastate.notEq("删除")
                , DMaterialBuyEntityDao.Properties.PGuid.eq((String) SPUtils.get(context, "project_guid", ""))).list();
        for (DMaterialBuyEntity d : dMaterialBuyEntities) {//工程量后面统一删除
            d.setMBDatastate("删除");
            if (d.getMBName().startsWith("(利旧)")) {
                DMaterialOldEntity load = GreenDaoManager.getInstance().getmDaoSession().getDMaterialOldEntityDao().load(d.getMMGuid());
                load.setDRWhere((Double.parseDouble(load.getDRWhere()) + d.getMBCount()) + "");
                GreenDaoManager.getInstance().getmDaoSession().getDMaterialOldEntityDao().update(load);
            }
        }
        session.getDMaterialBuyEntityDao().updateInTx(dMaterialBuyEntities);//删除新购材料

        List<DOtherCostEntity> dOtherCostEntities = session.getDOtherCostEntityDao().queryBuilder().where(
                DOtherCostEntityDao.Properties.PPGuid.eq(uuid)
                , DOtherCostEntityDao.Properties.CCDatastate.notEq("删除")
                , DOtherCostEntityDao.Properties.PGuid.eq((String) SPUtils.get(context, "project_guid", ""))).list();
        for (DOtherCostEntity d : dOtherCostEntities) {
            d.setCCDatastate("删除");
        }
        session.getDOtherCostEntityDao().updateInTx(dOtherCostEntities);//删除其他工程量

        List<DMaterialCostEntity> dMaterialCostEntities = session.getDMaterialCostEntityDao().queryBuilder().where(
                DMaterialCostEntityDao.Properties.PPGuid.eq(uuid)
                , DMaterialCostEntityDao.Properties.MCDatastate.notEq("删除")
                , DMaterialCostEntityDao.Properties.PGuid.eq((String) SPUtils.get(context, "project_guid", ""))).list();
        for (DMaterialCostEntity d : dMaterialCostEntities) {
            d.setMCDatastate("删除");
        }
        session.getDMaterialCostEntityDao().updateInTx(dMaterialCostEntities);//删除工程量
        List<DWorkContentEntity> dWorkContentEntities = session.getDWorkContentEntityDao().queryBuilder()
                .where(DWorkContentEntityDao.Properties.PGuid.eq((String) SPUtils.get(context, "project_guid", ""))
                        , DWorkContentEntityDao.Properties.PPGuid.eq((String) SPUtils.get(context, "UUID", "")))
                .list();
        for (DWorkContentEntity d : dWorkContentEntities) {
            d.setWCDatastate("删除");
        }
        session.getDWorkContentEntityDao().updateInTx(dWorkContentEntities);//删除工作内容
        context.finish();
    }

    public static void deleteYsPoint(Activity context) {
        DaoSession session = GreenDaoManager.getInstance().getmDaoSession();
        String uuid = (String) SPUtils.get(context, "UUID", "");
        EPointEntity pointEntity = session.getEPointEntityDao().load(uuid);
        long upCount = session.getEPointEntityDao().queryBuilder()
                .where(EPointEntityDao.Properties.PPUpguid.eq(uuid)
                        , EPointEntityDao.Properties.PGuid.eq((String) SPUtils.get(context, "project_ys_guid", ""))
                        , EPointEntityDao.Properties.PPDatastate.notEq("删除")).count();
        if (upCount > 0) {
            T.showLong(context, "该点位有下联点位，请先删除下联点位后再删除该点位");
            return;
        }
        long paCount = session.getEPointEntityDao().queryBuilder()
                .where(EPointEntityDao.Properties.PPParentguid.eq(uuid)
                        , EPointEntityDao.Properties.PGuid.eq((String) SPUtils.get(context, "project_ys_guid", ""))
                        , EPointEntityDao.Properties.PPDatastate.notEq("删除")).count();
        if (paCount > 0) {
            T.showLong(context, "该点位有子点位，请先删除子点位后再删除该点位");
            return;
        }
        List<EDeviceOldEntity> eDeviceOldEntities = session.getEDeviceOldEntityDao().queryBuilder().where(EDeviceOldEntityDao.Properties.PPGuid.eq(uuid)
                , EDeviceOldEntityDao.Properties.DRDatastate.notEq("删除")
                , EDeviceOldEntityDao.Properties.PGuid.eq((String) SPUtils.get(context, "project_ys_guid", ""))).list();
        for (EDeviceOldEntity d : eDeviceOldEntities) {
            d.setDRDatastate("删除");
            if (d.getDRWork().equals("拆除") && d.getDROpinion().equals("-9")) {
                T.showLong(context, "该点位拆除设备已被利旧，请先删除利旧设备");
                return;
            }
        }
        List<EMaterialOldEntity> eMaterialOldEntities = session.getEMaterialOldEntityDao().queryBuilder().where(EMaterialOldEntityDao.Properties.PPGuid.eq(uuid)
                , EMaterialOldEntityDao.Properties.MBDatastate.notEq("删除")
                , EMaterialOldEntityDao.Properties.PGuid.eq((String) SPUtils.get(context, "project_ys_guid", ""))).list();
        for (EMaterialOldEntity d : eMaterialOldEntities) {
            d.setMBDatastate("删除");
            if (d.getDROpinion().equals("10") && d.getMBCount() > Double.parseDouble(d.getDRWhere())) {
                T.showLong(context, "该点位有拆除材料已被利旧，请先删除利旧材料");
                return;
            }
        }
        session.getEMaterialOldEntityDao().updateInTx(eMaterialOldEntities);
        session.getEDeviceOldEntityDao().updateInTx(eDeviceOldEntities);


        pointEntity.setPPDatastate("删除");
        session.getEPointEntityDao().update(pointEntity);//删除点位
        List<DPictureEntity> dPictureEntities = session.getDPictureEntityDao().queryBuilder().where(DPictureEntityDao.Properties.PPGuid.eq(uuid)
                , DPictureEntityDao.Properties.PDatastate.notEq("删除")
                , DPictureEntityDao.Properties.PGuid.eq((String) SPUtils.get(context, "project_ys_guid", ""))).list();
        for (DPictureEntity d : dPictureEntities) {
            d.setPDatastate("删除");
        }
        session.getDPictureEntityDao().updateInTx(dPictureEntities);//删除点位照片

        List<EProblemexistEntity> eProblemexistEntities = session.getEProblemexistEntityDao().queryBuilder().where(EProblemexistEntityDao.Properties.Pointguid.eq(uuid)
                , EProblemexistEntityDao.Properties.Datastate.notEq("删除")
                , EProblemexistEntityDao.Properties.Projectguid.eq((String) SPUtils.get(context, "project_ys_guid", ""))).list();
        for (EProblemexistEntity d : eProblemexistEntities) {
            d.setDatastate("删除");
        }
        List<EDeviceBuyEntity> eDeviceBuyEntities = session.getEDeviceBuyEntityDao().queryBuilder().where(EDeviceBuyEntityDao.Properties.PPGuid.eq(uuid)
                , EDeviceBuyEntityDao.Properties.DBDatastate.notEq("删除")
                , EDeviceBuyEntityDao.Properties.PGuid.eq((String) SPUtils.get(context, "project_ys_guid", ""))).list();
        for (EDeviceBuyEntity d : eDeviceBuyEntities) {
            d.setDBDatastate("删除");
        }
        session.getEDeviceBuyEntityDao().updateInTx(eDeviceBuyEntities);

        List<EMaterialBuyEntity> eMaterialBuyEntities = session.getEMaterialBuyEntityDao().queryBuilder().where(EMaterialBuyEntityDao.Properties.PPGuid.eq(uuid)
                , EMaterialBuyEntityDao.Properties.MBDatastate.notEq("删除")
                , EMaterialBuyEntityDao.Properties.PGuid.eq((String) SPUtils.get(context, "project_ys_guid", ""))).list();
        for (EMaterialBuyEntity d : eMaterialBuyEntities) {
            d.setMBDatastate("删除");
        }
        session.getEMaterialBuyEntityDao().updateInTx(eMaterialBuyEntities);

        List<EWorkContentEntity> eWorkContentEntities = session.getEWorkContentEntityDao().queryBuilder().where(EWorkContentEntityDao.Properties.PPGuid.eq(uuid)
                , EWorkContentEntityDao.Properties.WCDatastate.notEq("删除")
                , EWorkContentEntityDao.Properties.PGuid.eq((String) SPUtils.get(context, "project_ys_guid", ""))).list();
        for (EWorkContentEntity d : eWorkContentEntities) {
            d.setWCDatastate("删除");
        }
        session.getEWorkContentEntityDao().updateInTx(eWorkContentEntities);
        context.finish();
    }

    /**
     * 作图的时候，插入导线相关信息
     *
     * @param context
     * @param b
     * @param span
     */
    public static void addWire(Context context, BMaterialEntity b, Double span) {
        b.setmCount(span);
        DMaterialBuyEntity unique = GreenDaoManager.getInstance().getmDaoSession().getDMaterialBuyEntityDao().queryBuilder().where(
                DMaterialBuyEntityDao.Properties.PGuid.eq(SPUtils.get(context, "project_guid", ""))
                , DMaterialBuyEntityDao.Properties.PPGuid.eq(SPUtils.get(context, "UUID", ""))
                , DMaterialBuyEntityDao.Properties.MBDatastate.notEq("删除")
                , DMaterialBuyEntityDao.Properties.MBSource.eq("系统")
        ).unique();//查询原始的导线信息
        if (unique == null) {
            unique = DBUtils.addBuyMaterial(context, b);
            unique.setMBSource("系统");
        } else {
            deleteCost(context, unique.getGuid());//删除工程量
            unique.setMMGuid(b.getGuid());
            unique.setMBBigtype(b.getMBigtype());
            unique.setMBMidtype(b.getMMidtype());
            unique.setMBName(b.getMName());
            unique.setDLabel(b.getMLabel());
            unique.setMBParameter(b.getMParameter());
            unique.setMBWeight(b.getMWeight());
            unique.setMBPrice(b.getMPrice());
            unique.setMBUnit(b.getMUnit());
            unique.setMBCount(b.getmCount());
            unique.setMBPriceall((b.getMPrice() == null ? 0 : b.getMPrice()) * b.getmCount());
            unique.setMBWeightall(unique.getMBCount() * (unique.getMBPrice() == null ? 0 : unique.getMBPrice()));
            unique.setMBSource("系统");
            unique.setMBDatastate("修改");
        }

        DBUtils.addMaterialCost(context, b.getGuid(), unique.getGuid(), b.getMName(), 1, 4, 1, span);//插入导线工程量
        GreenDaoManager.getInstance().getmDaoSession().getDMaterialBuyEntityDao().insertOrReplace(unique);//插入新导线
    }

    /**
     * 作图的时候，插入导线相关信息
     *
     * @param context
     * @param b
     * @param span
     */
    public static void addYsWire(Context context, BMaterialEntity b, Double span) {
        b.setmCount(span);
        EMaterialBuyEntity unique = GreenDaoManager.getInstance().getmDaoSession().getEMaterialBuyEntityDao().queryBuilder().where(
                EMaterialBuyEntityDao.Properties.PGuid.eq(SPUtils.get(context, "project_guid", ""))
                , EMaterialBuyEntityDao.Properties.PPGuid.eq(SPUtils.get(context, "UUID", ""))
                , EMaterialBuyEntityDao.Properties.MBDatastate.notEq("删除")
                , EMaterialBuyEntityDao.Properties.MBSource.eq("系统")
        ).unique();//查询原始的导线信息
        if (unique == null) {
            unique = DBUtils.addYsBuyMaterial(context, b);
            unique.setMBSource("系统");
        } else {
            unique.setMMGuid(b.getGuid());
            unique.setMBBigtype(b.getMBigtype());
            unique.setMBMidtype(b.getMMidtype());
            unique.setMBName(b.getMName());
            unique.setDLabel(b.getMLabel());
            unique.setMBParameter(b.getMParameter());
            unique.setMBWeight(b.getMWeight());
            unique.setMBPrice(b.getMPrice());
            unique.setMBUnit(b.getMUnit());
            unique.setMBCount(b.getmCount());
            unique.setMBPriceall((b.getMPrice() == null ? 0 : b.getMPrice()) * b.getmCount());
            unique.setMBWeightall(unique.getMBCount() * (unique.getMBPrice() == null ? 0 : unique.getMBPrice()));
            unique.setMBSource("系统");
            unique.setMBDatastate("修改");
        }

        GreenDaoManager.getInstance().getmDaoSession().getEMaterialBuyEntityDao().insertOrReplace(unique);//插入新导线
    }


    /**
     * 添加工作内容
     *
     * @param context
     */
    public static void addWorkContent(Context context) {
        /**
         * 保存工作内容
         */

        if (!SPUtils.get(context, "point_name", "").toString().equals("交叉跨越")) {
            DaoSession session = GreenDaoManager.getInstance().getmDaoSession();
            DPointEntity d = session.getDPointEntityDao().load((String) SPUtils.get(context, "UUID", ""));
            String r = session.getAPRangeEntityDao().load(d.getPRGuid()).getPRName() + ": "
                    + d.getPPName() + d.getPPCode();
            String content = r;
            String process = (String) SPUtils.get(context, "process", "");
            DWorkContentEntity dWorkContentEntity = session.getDWorkContentEntityDao().queryBuilder()
                    .where(DWorkContentEntityDao.Properties.PGuid.eq(SPUtils.get(context, "project_guid", ""))
                            , DWorkContentEntityDao.Properties.PPGuid.eq(SPUtils.get(context, "UUID", ""))).unique();
            if (dWorkContentEntity == null) {
                dWorkContentEntity = new DWorkContentEntity();
                dWorkContentEntity.setGuid(UUID.randomUUID().toString());
                dWorkContentEntity.setPGuid((String) SPUtils.get(context, "project_guid", ""));
                dWorkContentEntity.setPPGuid((String) SPUtils.get(context, "UUID", ""));
                dWorkContentEntity.setWCType(process);
                dWorkContentEntity.setWCDatastate("新增");
            } else {
                dWorkContentEntity.setWCDatastate("修改");
            }
            List<DDeviceBuyEntity> addDevice = session.getDDeviceBuyEntityDao().queryBuilder()
                    .where(DDeviceBuyEntityDao.Properties.PGuid.eq(SPUtils.get(context, "project_guid", ""))
                            , DDeviceBuyEntityDao.Properties.PPGuid.eq(SPUtils.get(context, "UUID", ""))
                            , DDeviceBuyEntityDao.Properties.DBDatastate.notEq("删除")).list();
            List<DDeviceOldEntity> list = session.getDDeviceOldEntityDao().queryBuilder()
                    .where(DDeviceOldEntityDao.Properties.PGuid.eq(SPUtils.get(context, "project_guid", ""))
                            , DDeviceOldEntityDao.Properties.PPGuid.eq(SPUtils.get(context, "UUID", ""))
                            , DDeviceOldEntityDao.Properties.DRDatastate.notEq("删除")).list();
            for (DDeviceOldEntity dd : list) {//旧设备
                content += "  " + (dd.getDRWork().equals("更换") ? "拆除" : dd.getDRWork()) + "  " + dd.getDRName();
                if (dd.getDRWork().equals("改造")) {
                    content += "  参数  " + dd.getDRParameter() + "  " + dd.getDRReformcontent() + "  为  " + dd.getDRReformparameter();
                }
                content += ";";
            }
            // TODO: 2018/09/30 待处理
            //tips:拆旧/更换点位时：如果"处置意见+处置去向"都不为空则增加1条项目内容和1条工程量
            for (DDeviceBuyEntity dd : addDevice) {
                content += "  新增  " + dd.getDBName() + ";";
            }
            if (!content.equals(r)) {
                dWorkContentEntity.setWCContent(content);
                session.getDWorkContentEntityDao().insertOrReplace(dWorkContentEntity);
            } else {
                session.getDWorkContentEntityDao().deleteByKey(dWorkContentEntity.getGuid());
            }
        }
    }

    /**
     * 添加工作内容
     *
     * @param context
     */
    public static void addYsWorkContent(Context context) {
        /**
         * 保存工作内容
         */

        if (!SPUtils.get(context, "point_name", "").toString().equals("交叉跨越")) {
            DaoSession session = GreenDaoManager.getInstance().getmDaoSession();
            EPointEntity d = session.getEPointEntityDao().load((String) SPUtils.get(context, "UUID", ""));
            String r = session.getEPRangeEntityDao().load(d.getPRGuid()).getPRName() + ": "
                    + d.getPPName() + d.getPPCode();
            String content = r;
            String process = (String) SPUtils.get(context, "process", "");
            EWorkContentEntity dWorkContentEntity = session.getEWorkContentEntityDao().queryBuilder()
                    .where(EWorkContentEntityDao.Properties.PGuid.eq(SPUtils.get(context, "project_ys_guid", ""))
                            , EWorkContentEntityDao.Properties.PPGuid.eq(SPUtils.get(context, "UUID", ""))).unique();
            if (dWorkContentEntity == null) {
                dWorkContentEntity = new EWorkContentEntity();
                dWorkContentEntity.setGuid(UUID.randomUUID().toString());
                dWorkContentEntity.setPGuid((String) SPUtils.get(context, "project_ys_guid", ""));
                dWorkContentEntity.setPPGuid((String) SPUtils.get(context, "UUID", ""));
                dWorkContentEntity.setWCType(process);
                dWorkContentEntity.setWCDatastate("新增");
            } else {
                dWorkContentEntity.setWCDatastate("修改");
            }
            List<EDeviceBuyEntity> addDevice = session.getEDeviceBuyEntityDao().queryBuilder()
                    .where(EDeviceBuyEntityDao.Properties.PGuid.eq(SPUtils.get(context, "project_ys_guid", ""))
                            , EDeviceBuyEntityDao.Properties.PPGuid.eq(SPUtils.get(context, "UUID", ""))
                            , EDeviceBuyEntityDao.Properties.DBDatastate.notEq("删除")).list();
            List<EDeviceOldEntity> list = session.getEDeviceOldEntityDao().queryBuilder()
                    .where(EDeviceOldEntityDao.Properties.PGuid.eq(SPUtils.get(context, "project_ys_guid", ""))
                            , EDeviceOldEntityDao.Properties.PPGuid.eq(SPUtils.get(context, "UUID", ""))
                            , EDeviceOldEntityDao.Properties.DRDatastate.notEq("删除")).list();
            for (EDeviceOldEntity dd : list) {//旧设备
                content += "  " + (dd.getDRWork().equals("更换") ? "拆除" : dd.getDRWork()) + "  " + dd.getDRName();
                if (dd.getDRWork().equals("改造")) {
                    content += "  参数  " + dd.getDRParameter() + "  " + dd.getDRReformcontent() + "  为  " + dd.getDRReformparameter();
                }
                content += ";";
            }
            // TODO: 2018/09/30 待处理
            //tips:拆旧/更换点位时：如果"处置意见+处置去向"都不为空则增加1条项目内容和1条工程量
            for (EDeviceBuyEntity dd : addDevice) {
                content += "  新增  " + dd.getDBName() + ";";
            }
            if (!content.equals(r)) {
                dWorkContentEntity.setWCContent(content);
                session.getEWorkContentEntityDao().insertOrReplace(dWorkContentEntity);
            } else {
                session.getEWorkContentEntityDao().deleteByKey(dWorkContentEntity.getGuid());
            }

        }
    }

    /**
     * 根据点位查询线路名称
     *
     * @param uuid
     * @return
     */
    public static String getLineName(String uuid) {
        DaoSession session = GreenDaoManager.getInstance().getmDaoSession();
        DPointEntity d = session.getDPointEntityDao().load(uuid);
        if (d == null) {
            DCrossCostEntity load = session.getDCrossCostEntityDao().load(uuid);
            APRangeEntity entity = session.getAPRangeEntityDao().load(load.getPRGuid());
            return entity.getPRName();
        }
        APRangeEntity a = session.getAPRangeEntityDao().load(d.getPRGuid());
        return a.getPRName();
    }

    /**
     * 根据点位查询线路名称
     *
     * @param uuid
     * @return
     */
    public static String getLineNameWithPPname(String uuid) {
        DaoSession session = GreenDaoManager.getInstance().getmDaoSession();
        DPointEntity d = session.getDPointEntityDao().load(uuid);
        if (d == null) {
            DCrossCostEntity load = session.getDCrossCostEntityDao().load(uuid);
            APRangeEntity entity = session.getAPRangeEntityDao().load(load.getPRGuid());
            return entity.getPRName();
        }
        APRangeEntity a = session.getAPRangeEntityDao().load(d.getPRGuid());
        return a.getPRName() + "(" + d.getPPCode() + ")";
    }

    /**
     * 根据点位查询线路名称
     *
     * @param uuid
     * @return
     */
    public static String getYsLineName(String uuid) {
        DaoSession session = GreenDaoManager.getInstance().getmDaoSession();
        EPointEntity d = session.getEPointEntityDao().load(uuid);
        if (d == null) {
            ECrossCostEntity load = session.getECrossCostEntityDao().load(uuid);
            EPRangeEntity entity = session.getEPRangeEntityDao().load(load.getPRGuid());
            return entity.getPRName();
        }
        EPRangeEntity a = session.getEPRangeEntityDao().load(d.getPRGuid());
        return a.getPRName();
    }

    /**
     * 根据点位查询线路名称
     *
     * @param uuid
     * @return
     */
    public static String getYsLineNameWithPPname(String uuid) {
        DaoSession session = GreenDaoManager.getInstance().getmDaoSession();
        EPointEntity d = session.getEPointEntityDao().load(uuid);
        if (d == null) {
            ECrossCostEntity load = session.getECrossCostEntityDao().load(uuid);
            EPRangeEntity entity = session.getEPRangeEntityDao().load(load.getPRGuid());
            return entity.getPRName();
        }
        EPRangeEntity a = session.getEPRangeEntityDao().load(d.getPRGuid());
        return a.getPRName() + "(" + d.getPPCode() + ")";
    }

    /**
     * 根据点位查询线路名称
     *
     * @param uuid
     * @return
     */
    public static String getYsLineNameAndPoint(String uuid) {
        DaoSession session = GreenDaoManager.getInstance().getmDaoSession();
        EPointEntity d = session.getEPointEntityDao().load(uuid);
        if (d == null) {
            ECrossCostEntity load = session.getECrossCostEntityDao().load(uuid);
            EPRangeEntity entity = session.getEPRangeEntityDao().load(load.getPRGuid());
            return entity.getPRName() + "(" + "交叉跨越-" + load.getCCName() + ")";
        }
        EPRangeEntity a = session.getEPRangeEntityDao().load(d.getPRGuid());
        return a.getPRName() + "(" + d.getPPName() + "-" + d.getPPCode() + ")";
    }

}
